package view.panel
{
	import control.panel.IPanelControl;
	import control.panel.RewardPanelControl;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import utils.DisplayTool;
	import utils.TextTool;
	
	import view.LabelText;
	import view.components.ImgButton;
	import view.components.Input;
	
	public class RewardPanel extends AbsPanel
	{
		private var input:Input;
		private var inputLayer:Sprite;
		private var hintLayer:Sprite;
		private var hintText:TextField;
		private var money:TextField;
		private var code:DisplayObject;
		
		private var refreshBut:ImgButton;
		
		public function RewardPanel(panelControl:IPanelControl)
		{
			super(panelControl);
			resetBg(300,150,1,true);
			initCloseBut();
			resetCloseBut();
			initTitle("text_title_reward");
			
			initTweenPanelOpen();
			dropShadow();
			
			initLayer();
			initText();
			initInput();
			initBut();
		}
		
		private function initLayer():void
		{
			inputLayer = new Sprite();
			hintLayer = new Sprite();
			
			addChild(inputLayer);
			//addChild(hintLayer);
		}
		
		private function initBut():void
		{
			refreshBut = new ImgButton(213,49,0,onBut,50,24,"butBg",null,"刷新");
			inputLayer.addChild(refreshBut);
			inputLayer.addChild(new ImgButton(213,80,1,onBut,50,24,"butBg",null,"确定"));
			
			hintLayer.addChild(new ImgButton(125,103,2,onBut,50,24,"butBg",null,"确定"));
		}
		
		private function onBut(event:MouseEvent):void
		{
			switch(event.target.id)
			{
				case 0:
					event.target.mouseEnabled = false;
					(panelControl as RewardPanelControl).refresh();
					refreshBut.mouseEnabled = false;
					break;
				case 1:
					(panelControl as RewardPanelControl).onSendVeryCode(input.text);
					break;
				case 2:
					close();
					break;
			}
		}
		
		private function initInput():void
		{
			input = new Input(99,22,0xFFFFFF,true,"center",14);
			input.editable = true;
			input.x = 90;
			input.y = 81;
			input.maxChars = 4;
			input.textField.restrict = "0-9";
			inputLayer.addChild(input);
		}
		
		private function initText():void
		{
			inputLayer.addChild(new LabelText(80,25,150,30,"请输入验证码:",0xFFFFFF,18,true));
			
			hintLayer.addChild(new LabelText(90,30,150,20,"你已成功获取今日奖金"));
			
			money = TextTool.getTextField(120,30,20,0xFFFF00,false,true,"center");
			money.x = 90;
			money.y = 60;
			//money.text = "$99999";
			hintLayer.addChild(money);
			
			hintText = new LabelText(70,110,150,20,"",0xAAAAAA);
			inputLayer.addChild(hintText);
		}
		
		public function updateHint(text:String):void
		{
			hintText.text = text;
		}
		
		public function updateMoney(text:String):void
		{
			money.text = "$" + text;
		}
		
		public function addVeryCode(code:DisplayObject):void
		{
			
			refreshBut.mouseEnabled = true;
			DisplayTool.removeChild(inputLayer,code);
			code.x = 90;
			code.y = 50;
			this.code = code;
			
			
			inputLayer.addChild(code);
		}
		
		public function switchLayer(type:int):void
		{
			if (type == 0)
			{
				DisplayTool.removeChild(this,hintLayer);
				addChild(inputLayer);
			}
			else
			{
				DisplayTool.removeChild(this,inputLayer);
				addChild(hintLayer);
			}
		}
		
		override public function close() : void
		{
			super.close();
			switchLayer(0);
		}
		
		override public function open():void
		{
			super.open();
			refreshBut.mouseEnabled = true;
			input.text = "";
			stage.focus = input.textField;
			DisplayTool.removeChild(inputLayer,code);
		}
		
	}
}